/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.guesser.utils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class ThinnedChecker{
    public static HashMap doCheck(String chain_code) {
        HashMap<String, String> cache = new HashMap<>();
        
        cache.put("Chain code",         getChainCode(chain_code));
        cache.put("Sequence",           getSequence(chain_code));
        cache.put("Enhanced",           getEnhancedSequence(getSequence(chain_code)));
        cache.put("Position",           getPosition(chain_code));
        cache.put("Titik",              isDotted(chain_code));
        cache.put("Garis Horizontal",   "" + cekGarisLurus(getChainCode(chain_code))[0]);
        cache.put("Garis Vertical",     "" + cekGarisLurus(getChainCode(chain_code))[1]);
        cache.put("Garis Miring",       "" + cekGarisMiring(getSequence(chain_code)));
        cache.put("Cembung Kanan",      "" + cekKurvaCembungKanan(getSequence(chain_code)));
        cache.put("Cembung Kiri",       "" + cekKurvaCembungKiri(getSequence(chain_code)));
        cache.put("Cembung Atas",       "" + cekKurvaCembungAtas(getSequence(chain_code)));
        cache.put("Cembung Bawah",      "" + cekKurvaCembungBawah(getSequence(chain_code)));
        return cache;
    }
    
    private static String getPosition(String chain_code) {
        //if (chain_code.contains("titik")) {
        //    return ("(" + chain_code.substring(chain_code.indexOf("+") + 1, chain_code.indexOf("X")) + "," + chain_code.substring(chain_code.indexOf("X") + 1, chain_code.indexOf("Y")) + ")");
        //} else {
            return ("(" + chain_code.substring(0, chain_code.indexOf("X")) + "," + chain_code.substring(chain_code.indexOf("X") + 1, chain_code.indexOf("Y")) + ")");
        //}
    }
    
    private static String getChainCode(String chain_code) {
        String cache = "";
        
        if (chain_code.contains("titik")) {
            cache = chain_code.substring(chain_code.indexOf("+") + 1);
        } else {
            cache = chain_code.substring(chain_code.indexOf("Y") + 1);
        }
        
        
        String code  = "";
        
        if (cache.charAt(cache.length() - 1) == '+') {
            cache = cache.substring(0, cache.length() - 1);
        }
        
        String[] splitted = cache.split("\\+");
        
        for (int i = 0; i < splitted.length; i++) {
            //System.out.println(splitted[i]);
            if (splitted[i].length() > 7) {
                code += splitted[i] + "-";
            }
        } // */
        
        return code;
    }
    
    private static String isDotted(String chain_code) {
        if (chain_code.contains("titik")) {
            return "1";
        } else {
            return "0";
        }
    }
    
    private static int[] getFrequency(String chain_code) {
        int[] frequency = new int[9];
        
        String code = chain_code.substring(chain_code.indexOf("Y") + 1);
        
        for (int i = 0; i < code.length(); i++) {
            frequency[code.charAt(i) - 49]++;
        }
        
        return frequency;
    }
    
    public static String getSequence(String chain_code) {
        String code = getChainCode(chain_code);
        //System.out.println("Chain Code : " + code);
        String sequence = "";
        
        sequence += code.charAt(0);
        for (int i = 1; i < code.length(); i++) {
            if (sequence.charAt(sequence.length() - 1) != code.charAt(i)) {
                sequence += code.charAt(i);
            }
        }
       
        //System.out.println("Before : " + sequence);
        
        return givePutty(sequence);
    }
    
    public static String getEnhancedSequence(String sequence) {
        // 12 = A; 14 = B; 32 = C; 36 = D; 74 = E; 78 = F; 96 = G; 98 = H; 
        
        
        String enhanced     = "";
        String current      = "";
        
        String newSequence  = "XX" + sequence + "XX";

        for (int i = 0; i < newSequence.length(); i++) {
            current = "" + newSequence.charAt(i);
           
            switch(current) {
                case "1":
                    if (newSequence.charAt(i + 1) == '2'){
                        newSequence = getNewSequence(newSequence, i, "12", '3', "A");
                    } else if (newSequence.charAt(i + 1) == '4') {
                        newSequence = getNewSequence(newSequence, i, "14", '7', "B");
                    } else {
                        
                    }
                    break;
                case "2":
                    if (newSequence.charAt(i + 1) == '1'){
                        newSequence = getNewSequence(newSequence, i, "21", '4', "A");
                    } else if (newSequence.charAt(i + 1) == '3') {
                        newSequence = getNewSequence(newSequence, i, "23", '6', "C");
                    } else if (newSequence.charAt(i + 1) == '4') {
                        newSequence = getNewSequence(newSequence, i, "24", '2', "AB");
                    } else if (newSequence.charAt(i + 1) == '6') {
                        newSequence = getNewSequence(newSequence, i, "26", '2', "CD");
                    } else {
                        
                    }
                    break; 
                case "3":
                    if (newSequence.charAt(i + 1) == '2'){
                        newSequence = getNewSequence(newSequence, i, "32", '1', "C");
                    } else if (newSequence.charAt(i + 1) == '6') {
                        newSequence = getNewSequence(newSequence, i, "36", '9', "D");
                    } else {
                        
                    }
                    break;
                case "4":
                    if (newSequence.charAt(i + 1) == '1'){
                        newSequence = getNewSequence(newSequence, i, "41", '7', "B");
                    } else if (newSequence.charAt(i + 1) == '7') {
                        newSequence = getNewSequence(newSequence, i, "47", '8', "E");
                    } else if (newSequence.charAt(i + 1) == '2') {
                        newSequence = getNewSequence(newSequence, i, "42", '4', "BA");
                    } else if (newSequence.charAt(i + 1) == '8') {
                        newSequence = getNewSequence(newSequence, i, "48", '4', "EF");
                    } else {
                        
                    }
                    break; 
                case "6":
                    if (newSequence.charAt(i + 1) == '3'){
                        newSequence = getNewSequence(newSequence, i, "63", '2', "D");
                    } else if (newSequence.charAt(i + 1) == '9') {
                        newSequence = getNewSequence(newSequence, i, "69", '8', "G");
                    } else if (newSequence.charAt(i + 1) == '2') {
                        newSequence = getNewSequence(newSequence, i, "62", '6', "DC");
                    } else if (newSequence.charAt(i + 1) == '8') {
                        newSequence = getNewSequence(newSequence, i, "68", '6', "GH");
                    } else {
                        
                    }
                    break;
                case "7":
                    if (newSequence.charAt(i + 1) == '4'){
                        newSequence = getNewSequence(newSequence, i, "74", '1', "E");
                    } else if (newSequence.charAt(i + 1) == '8') {
                        newSequence = getNewSequence(newSequence, i, "78", '9', "F");
                    } else {
                        
                    }
                    break;
                case "8":
                    if (newSequence.charAt(i + 1) == '7'){
                        newSequence = getNewSequence(newSequence, i, "87", '4', "F");
                    } else if (newSequence.charAt(i + 1) == '9') {
                        newSequence = getNewSequence(newSequence, i, "89", '6', "H");
                    } else if (newSequence.charAt(i + 1) == '4') {
                        newSequence = getNewSequence(newSequence, i, "84", '8', "FE");
                    } else if (newSequence.charAt(i + 1) == '6') {
                        newSequence = getNewSequence(newSequence, i, "86", '8', "HG");
                    } else {
                        
                    }
                    break;
                case "9":
                    if (newSequence.charAt(i + 1) == '6'){
                        newSequence = getNewSequence(newSequence, i, "96", '3', "G");
                    } else if (newSequence.charAt(i + 1) == '8') {
                        newSequence = getNewSequence(newSequence, i, "98", '7', "H");
                    } else {
                        
                    }
                    break; 
                default:
                    
                    break;
                    
            }
        }
        
        enhanced = newSequence.substring(2, newSequence.length() - 2);
        
        return ensure(tidyUp(enhanced));
        //return (tidyUp(enhanced));
    }
    
    private static int[] cekGarisLurus(String code) {
        int[] garisLurus = new int[2];
        int counter = 1;
        
        int garis_horizontal = 0;
        int garis_vertical   = 0;
        
        //String patched = flatten(code);
        String patched = code;
        int current = 0;
        int prev    = 0;
        
        //System.out.println(patched);
        
        for(int i = 1; i < patched.length(); i++) {
            current = (int)patched.charAt(i);
            prev    = (int)patched.charAt(i-1);
            if ((current == prev) && (current % 2 == 0)) {
                counter++;
            } else {
                if (counter > 7) {
                    if ((prev == 50) && (prev == 56)) {
                        garis_vertical++;
                    } else if ((prev == 52) && (prev == 54)) {
                        garis_horizontal++;
                    } else {
                        
                    }
                }
                counter = 1;
            }
        }
        
        if (counter > 7) {
            if ((prev == 50) && (prev == 56)) {
                garis_vertical++;
            } else if ((prev == 52) && (prev == 54)) {
                garis_horizontal++;
            } else {

            }
            counter = 1;
        }

        garisLurus[0] = garis_horizontal;
        garisLurus[1] = garis_vertical;
        
        return garisLurus;
    }
    
    private static int cekGarisMiring(String sequence) {
        int garisMiring = 0;
        
        String enhanced = getEnhancedSequence(sequence).replaceAll("-", "");
        
        for (int i = 0; i < enhanced.length(); i++) {
            if ((int)enhanced.charAt(i) > 64) {
                garisMiring++;
            }
        }
        
        return garisMiring;
    }
    
    private static int cekKurvaCembungKanan(String sequence) {
        int kurvaCembungKanan = 0;
        
        String enhanced    = getEnhancedSequence(sequence);
        String[] letter    = getLetter(enhanced);
        
//        System.out.println(enhanced);
        
        kurvaCembungKanan += runKurva(letter, enhanced, "GHFEDCAB", "98743214");
 
        return kurvaCembungKanan;
    }
    
    private static int cekKurvaCembungKiri(String sequence) {
        int kurvaCembungKiri = 0;
        
        String enhanced    = getEnhancedSequence(sequence);
        String[] letter    = getLetter(enhanced);
        
//        System.out.println(enhanced);
        
        kurvaCembungKiri += runKurva(letter, enhanced, "BACDEFHG", "12367896");
        
        return kurvaCembungKiri;
        
    }
    
    private static int cekKurvaCembungAtas(String sequence) {
        int kurvaCembungAtas = 0;
        
        String enhanced    = getEnhancedSequence(sequence);
        String[] letter    = getLetter(enhanced);
        
//        System.out.println(enhanced);
        
        kurvaCembungAtas += runKurva(letter, enhanced, "CDGHABEF", "36981478");
        
        return kurvaCembungAtas;
        
    }
        
    private static int cekKurvaCembungBawah(String sequence) {
        int kurvaCembungBawah = 0;
        
        String enhanced    = getEnhancedSequence(sequence);
        String[] letter    = getLetter(enhanced);
        
//        System.out.println(enhanced);
        
        kurvaCembungBawah += runKurva(letter, enhanced, "FEBAHGDC", "74129632");
        
        return kurvaCembungBawah;
        
    }
       
    private static String givePutty(String code) {
        String patched = "XXX" + code + "XXX";
        
        String current = "";
        String prev1   = "";
        String prev2   = "";
        String next    = "";
        
        int iteration = 0;
        
        for (int i = 2; i < patched.length() - 1; i++) {
            current = "" + patched.charAt(i);
            if (patched.charAt(i-1) == '-') {
                
                prev1   = "" + patched.charAt(i+1);
                prev2   = "" + patched.charAt(i+2);
                iteration = 0;
                while (!(prev1.equals("-")) && !(prev2.equals("-")) && !(prev1.equals("X")) && !(prev2.equals("X"))) {
                    iteration++;
                    if ((patched.charAt(i+1+(iteration*2)) == prev1.charAt(0)) && (patched.charAt(i+2+(iteration*2)) == prev2.charAt(0))) {
                        
                    } else {
                        prev1 = "" + patched.charAt(i+1+(iteration*2));
                        prev2 = "" + patched.charAt(i+2+(iteration*2));
                        
                        break;
                    }
                }
            } else {
                prev1   = "" + patched.charAt(i-1);
                prev2   = "" + patched.charAt(i-2);                
            } // */
            
            //prev1   = "" + patched.charAt(i-1);
            //prev2   = "" + patched.charAt(i-2);
            next    = "" + patched.charAt(i+1);
            
            switch(current){
                case "2":
                    if (prev1.equals("4") && prev2.equals("1") && next.equals("4")) {
                        patched = patched.substring(0,i) + "1" + patched.substring(i+1);
                    } else if (prev1.equals("6") && prev2.equals("3") && next.equals("6")) {
                        patched = patched.substring(0,i) + "3" + patched.substring(i+1);
                    }                    
                    break;
                case "4":
                    if (prev1.equals("8") && prev2.equals("7") && next.equals("8")) {
                        patched = patched.substring(0,i) + "7" + patched.substring(i+1);
                    } else if (prev1.equals("2") && prev2.equals("1") && next.equals("2")) {
                        patched = patched.substring(0,i) + "1" + patched.substring(i+1);
                    }
                    break;
                case "6":
                    if (prev1.equals("2") && prev2.equals("3") && next.equals("2")) {
                        patched = patched.substring(0,i) + "3" + patched.substring(i+1);
                    } else if (prev1.equals("8") && prev2.equals("9") && next.equals("8")) {
                        patched = patched.substring(0,i) + "9" + patched.substring(i+1);
                    }                    
                    break;
                case "8":
                    if (prev1.equals("4") && prev2.equals("7") && next.equals("4")) {
                        patched = patched.substring(0,i) + "7" + patched.substring(i+1);
                    } else if (prev1.equals("6") && prev2.equals("9") && next.equals("6")) {
                        patched = patched.substring(0,i) + "9" + patched.substring(i+1);
                    }                    
                    break;
                default:
                    
                    break;
            }
            
            
        }
        
        return patched.replace("XXX", "");
    }
    
    private static String flatten(String code) {
        int  counter = 1;
        char prev;
        
        String flatted = code;
        
        System.out.println(flatted);
        
        for(int i = 1; i < flatted.length() - 3; i++) {
            prev = flatted.charAt(i - 1);
           // System.out.print(counter + " --> " + prev + " = " + flatted.charAt(i));
            if (flatted.charAt(i) == '-'){
                counter = 1;
            } else if (prev == flatted.charAt(i)) {
                //System.out.println(" !!!!");
                counter++;
            } else {
                //System.out.println("");
                if (counter > 4) {
                    if ((flatted.charAt(i + 1) == prev) && (flatted.charAt(i + 2) == prev)) {
                        flatted = flatted.substring(0,i) + prev + flatted.substring(i + 1);
                        i++; i++;
                    }
                }
                if (flatted.charAt(i + 1) == prev) {
                            
                } else {
                    counter = 1;
                }
            }
        }
        
        return flatted;
    }
    
    private static String getNewSequence(String sequence, int index, String pattern, char another, String subtitution) {
    
        boolean flag = true;
        String  newSequence = "";
        int     counter = 0;
        
        while (flag) {
            if (sequence.substring(index+counter, index+counter+2).equals(pattern)) {
                counter++; counter++;
            } else {
                if (counter >= 4) {
                    if ((sequence.charAt(index+counter) == pattern.charAt(0))) {
                        if (isSequence("" + sequence.charAt(index+counter) + sequence.charAt(index+counter+1)) && (sequence.charAt(index+counter+2) == pattern.charAt(0))) {
                            
                        } else {
                            counter++;
                        }
                    } else {
                        if (isSequence("" + sequence.charAt(index+counter-1) + sequence.charAt(index+counter)) && (sequence.charAt(index+counter+1) == pattern.charAt(1))) {
                            counter--;
                        } else {
                            
                        }
                    }
                    
                    newSequence = sequence.substring(0,index) + subtitution + sequence.substring(index+counter);
                }
                flag = false;
            }
        }
        
        if (newSequence.isEmpty()) {
            return sequence;
        } else {
            return newSequence;
        }
        
    }
        
    private static String[] getLetter(String enhanced) {
        
        ArrayList<String> letter = new ArrayList<>();

        for (int i = 0; i < enhanced.length(); i++) {
            if ((int)enhanced.charAt(i) > 64) {
                letter.add("" + enhanced.charAt(i) + i);
            }
        }
        
        String[] cache = new String[letter.size()];
        cache = letter.toArray(cache);
        
        return cache;
    }

    private static String getLetterArray(String enhanced) {
        
       String letter = "";

        for (int i = 0; i < enhanced.length(); i++) {
            if ((int)enhanced.charAt(i) > 64) {
                letter += enhanced.charAt(i);
            }
        }

        return letter;
    }
    
    private static int runKurva (String[] letter, String enhanced, String pattern, String infix) {
        int kurva = 0;
        int prev  = 0;
        
        String order = "";
        
        String first_pattern    = pattern.substring(0,3);
        String second_pattern   = pattern.substring(1,4);
        String third_pattern    = pattern.substring(4,7);
        String fourth_pattern   = pattern.substring(5,8);
        
        String first_infix      = infix.substring(0,3);
        String second_infix     = infix.substring(1,4);
        String third_infix      = infix.substring(4,7);
        String fourth_infix     = infix.substring(5,8);
        
        /*
        System.out.println(first_pattern);
        System.out.println(second_pattern);
        System.out.println(third_pattern);
        System.out.println(fourth_pattern); // */
        
        for (int i = 0; i < enhanced.length(); i++) {
              if (enhanced.length() - i > 2) {
                order = enhanced.substring(i, i+3);
            } else {
                order = "";
            }
            
            if (enhanced.charAt(i) == first_pattern.charAt(0)) {
                if (order.equals(first_pattern)) {
                    kurva++;
                    i++;
                } else {
                    kurva += cekKurva(letter, enhanced, i, first_pattern, first_infix, second_pattern.charAt(2));
                }
            } else if (enhanced.charAt(i) == second_pattern.charAt(0)) {
                if (order.equals(second_pattern)) {
                    kurva++;
                    i++;
                } else {
                    kurva += cekKurva(letter, enhanced, i, second_pattern, second_infix, 'Z');
                }
            } else if (enhanced.charAt(i) == third_pattern.charAt(0)) {
                if (order.equals(third_pattern)) {
                    kurva++;
                    i++;
                } else {
                    kurva += cekKurva(letter, enhanced, i, third_pattern, third_infix, fourth_pattern.charAt(2));
                }
            } else if (enhanced.charAt(i) == fourth_pattern.charAt(0)) {
                if (order.equals(fourth_pattern)) {
                    kurva++;
                    i++;
                } else {
                    kurva += cekKurva(letter, enhanced, i, fourth_pattern, fourth_infix, 'Z');
                }
            } else {
                
            }
        }
        
        return kurva;
    }
    
    private static int cekKurva (String[] letter, String enhanced, int index, String pattern, String infix, char nuisance) {
        int kurva = 0;
        
        char first  = pattern.charAt(0);
        char second = pattern.charAt(1);
        char third  = pattern.charAt(2);
        
        String middle       = "";
        String nextLetter   = getNextLetter(letter, ("" + first +index));
        String letterArray  = getLetterArray(enhanced);
        
        int indexLetter         = getIndexLetter(letter, ("" + first +index));
        int nextLetterPosition  = 0;
        
        if (!(nextLetter.isEmpty())) {
            nextLetterPosition = Integer.parseInt(nextLetter.substring(1));
        }

        if ((letter.length - indexLetter) == 1) { // Index terakhir
            
        } else if ((letter.length - indexLetter) >= 2) { // 2 dari belakang
            if ((letterArray.charAt(indexLetter+1) == third) && (enhanced.charAt(index+1) != third)) {
                middle = enhanced.substring(index+1, nextLetterPosition);
                kurva += cekMiddle(middle, infix);
            } else if (enhanced.charAt(index + 1) == third) {
                kurva++;
            } else if (enhanced.charAt(index + 1) == second) {
                nextLetter   = getNextLetter(letter, (nextLetter));
                if (nextLetter.isEmpty()) {
                    nextLetterPosition = enhanced.length();
                } else {
                    nextLetterPosition = Integer.parseInt(nextLetter.substring(1));
                }
                middle = enhanced.substring(index+2, nextLetterPosition);
                
                if (middle.contains("-")) {
                    middle = middle.substring(0,middle.indexOf("-"));
                }
                if (middle.contains(infix.substring(1))) {
                    kurva++;
                }
            } else {
                if (nextLetter.isEmpty()) {
                    nextLetterPosition = enhanced.length();
                } else {
                    nextLetterPosition = Integer.parseInt(nextLetter.substring(1));
                }
                middle = enhanced.substring(index+1, nextLetterPosition);
                
                if (middle.contains("-")) {
                    middle = middle.substring(0,middle.indexOf("-"));
                }
                if (middle.contains(infix.substring(1))) {
                    kurva++;
                }
            }
        } 
        
        return kurva;
    }

    private static String getNextLetter(String[] letter, String current) {
        
        for (int i = 0; i < letter.length - 1; i++) {
            if (letter[i].equals(current)) {
                return letter[i+1];
            }
        }
        
        return "";
    }
    
    private static int getIndexLetter(String[] letter, String current) {
        
        for (int i = 0; i < letter.length; i++) {
            if (letter[i].equals(current)) {
                return i;
            }
        }
        
        return 999;
    }

    private static int cekMiddle (String middle, String contain) {
        int addition = 0;
        
        if (((middle.contains("" + contain.charAt(0))) || (middle.contains("" + contain.charAt(1))) || middle.contains("" + contain.charAt(2))) && !(middle.contains("-"))) {
            addition++;
        }
        
        return addition;
    }
    
    private static String tidyUp (String sequence) {
        
        //System.out.println(sequence);
        String newSequence = sequence;
        
        for (int i = 1; i < (newSequence.length()); i++) {
            //System.out.println(i + ", " + newSequence);
            /*
            if (((int)newSequence.charAt(i) > 64) && ((int)newSequence.charAt(i+1) < 64) && ((int)newSequence.charAt(i+2) > 64)) {
                newSequence = newSequence.substring(0,i+1) + newSequence.substring(i+2);
            } // */
            if (((int)newSequence.charAt(i) > 64) && (newSequence.charAt(i) == newSequence.charAt(i-1))) {
                newSequence = newSequence.substring(0,i) + newSequence.substring(i+1);
                i--;
            }
            
        }
        
        return newSequence;
    }
    
    private static boolean isSequence (String pattern) {
        return ((pattern.equalsIgnoreCase("12")) ||
                (pattern.equalsIgnoreCase("14")) ||
                (pattern.equalsIgnoreCase("21")) ||
                (pattern.equalsIgnoreCase("23")) ||
                (pattern.equalsIgnoreCase("32")) ||
                (pattern.equalsIgnoreCase("36")) ||
                (pattern.equalsIgnoreCase("41")) ||
                (pattern.equalsIgnoreCase("47")) ||
                (pattern.equalsIgnoreCase("63")) ||
                (pattern.equalsIgnoreCase("69")) ||
                (pattern.equalsIgnoreCase("74")) ||
                (pattern.equalsIgnoreCase("78")) ||
                (pattern.equalsIgnoreCase("87")) ||
                (pattern.equalsIgnoreCase("89")) ||
                (pattern.equalsIgnoreCase("96")) ||
                (pattern.equalsIgnoreCase("98")) ||
                (pattern.equalsIgnoreCase("24")) ||
                (pattern.equalsIgnoreCase("26")) ||
                (pattern.equalsIgnoreCase("42")) ||
                (pattern.equalsIgnoreCase("48")) ||
                (pattern.equalsIgnoreCase("62")) ||
                (pattern.equalsIgnoreCase("68")) ||
                (pattern.equalsIgnoreCase("84")) ||
                (pattern.equalsIgnoreCase("86")));
    }
    
    public static String ensure (String enhanced) {
        int      index_accused = 0;
        int      index_letter  = 0;
        int      iteration     = 0;
        String   newEnhanced = "X" + enhanced + "X";
        String   letter      = getLetterArray(newEnhanced);
        String   neighbours  = "";
        String[] listAccused = containDouble(newEnhanced);
        
        for (int i = 0; i < listAccused.length; i++) {
            index_accused = Integer.parseInt(listAccused[i].substring(2)) + iteration*2;
            index_letter  = letter.indexOf(listAccused[i].substring(0,2));
            if (index_letter < 0) {
                
            } else {
                neighbours = letter.charAt(index_letter-1) + listAccused[i].substring(0,2) + letter.charAt(index_letter+2);
                if (lookAround(neighbours)) {

                } else {
                    newEnhanced = newEnhanced.substring(0, index_accused) + doubleReplecer(listAccused[i].substring(0,2)) + newEnhanced.substring(index_accused+2);
                    letter      = getLetterArray(newEnhanced);
                    iteration++;
                } 
            }// */
        }
        
        return newEnhanced.replace("X", "");
    }
    
    private static String[] containDouble(String accused) {
        ArrayList<String> cache = new ArrayList<>();
        
        String pattern = "";
        
        for(int i = 0; i < accused.length() - 1; i++) {
            pattern = "" + accused.charAt(i) + accused.charAt(i+1);
            if (inListDouble(pattern)) {
                cache.add(pattern+i);
            }
        }
        
        String[] result = cache.toArray(new String[cache.size()]);
        
        return result;
    }
    
    private static boolean inListDouble (String accused) {
        return ((accused.equals("AB")) ||
                (accused.equals("BA")) ||
                (accused.equals("CD")) ||
                (accused.equals("DC")) ||
                (accused.equals("EF")) ||
                (accused.equals("FE")) ||
                (accused.equals("GH")) ||
                (accused.equals("HG")));
    }
    
    private static boolean lookAround (String accused) {
        String pattern = accused.substring(1,3);
        String prev    = "" + accused.charAt(0);
        String next    = "" + accused.charAt(3);
        
        switch(pattern) {
            case "AB":
                return ((prev.equals("D")) || (prev.equals("C")) || (next.equals("E")) || (next.equals("F")));
            case "BA":
                return ((prev.equals("F")) || (prev.equals("E")) || (next.equals("C")) || (next.equals("D")));
            case "CD":
                return ((prev.equals("B")) || (prev.equals("A")) || (next.equals("G")) || (next.equals("H")));
            case "DC":
                return ((prev.equals("H")) || (prev.equals("G")) || (next.equals("A")) || (next.equals("B")));
            case "EF":
                return ((prev.equals("A")) || (prev.equals("B")) || (next.equals("H")) || (next.equals("G")));
            case "FE":
                return ((prev.equals("G")) || (prev.equals("H")) || (next.equals("B")) || (next.equals("A")));
            case "GH":
                return ((prev.equals("C")) || (prev.equals("D")) || (next.equals("F")) || (next.equals("E")));
            case "HG":
                return ((prev.equals("E")) || (prev.equals("F")) || (next.equals("D")) || (next.equals("C")));
            default:
                return false;
        }
    }
    
    private static String doubleReplecer (String accused) {
        switch(accused) {
            case "AB":
                return "2424";
            case "BA":
                return "4242";
            case "CD":
                return "2626";
            case "DC":
                return "6262";
            case "EF":
                return "4848";
            case "FE":
                return "8484";
            case "GH":
                return "6868";
            case "HG":
                return "8686";
            default:
                return "";
        }
    }
    
}
